package net.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class RWFile extends Tools{
	
	/**
	 * 新增一条
	 * @param filename
	 * @param key
	 * @param value
	 * @param common
	 */
	public static void wProperties(String filename, String key, String value, String common){
		
		Properties prop = new Properties();
		try {
			InputStream inStream = new FileInputStream(filename);
			prop.load(inStream);
			inStream.close();
			OutputStream out = new FileOutputStream(filename);
			prop.setProperty(key, value);
			prop.store(out, common);
			out.flush();
			out.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	
	/**
	 * 新增多条
	 * @param filename
	 * @param list 格式为 key,value
	 * @param common
	 */
	public static void wProperties(String filename, List<String[]> list, String common){
		
		Properties prop = new Properties();
		try {
			InputStream inStream = new FileInputStream(filename);
			prop.load(inStream);
			inStream.close();
			OutputStream out = new FileOutputStream(filename);
			setValues(prop,list);
			prop.store(out, common);
			out.flush();
			out.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	
	
	/**
	 * 重写文件内容
	 * @param filename
	 * @param list
	 * @param common 文件描述
	 */
	public static void reProperties(String filename, List<String[]> list, String common){
		
		Properties prop = new Properties();
		try {
			OutputStream out = new FileOutputStream(filename);
			setValues(prop,list);
			prop.store(out, common);
			out.flush();
			out.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	
	/**
	 * 获取文件全部内容
	 * @param filename
	 * @return
	 */
	public static Map<String, String> getProperties(String filename){
		
		Properties prop = new Properties();
		Map<String, String> map = new HashMap<String, String>();
		String key, value = "";
		try {
			InputStream inStream = new FileInputStream(filename);
			prop.load(inStream);
			Enumeration en = prop.propertyNames();
			while (en.hasMoreElements()) {
				key = (String) en.nextElement();
				value = prop.getProperty(key);
				map.put(key, value);
			}
			inStream.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		return map;
	}
	
	public static String lProperties(String filename,String key){
		
		Properties prop = new Properties();
		Map<String, String> map = new HashMap<String, String>();
		try {
			InputStream inStream = new FileInputStream(filename);
			prop.load(inStream);
			inStream.close();
			return prop.getProperty(key);
		} catch (Exception e) {
			System.out.println(e);
		}
		return null;
	}	
	
	/**
	 * 设置值
	 * @param prop
	 * @param list
	 */
	private static void setValues(Properties prop, List<String[]> list){
		if(empty(prop)&&empty(list)) return;
		for(String[] param : list.toArray(new String[list.size()][])){
			prop.setProperty(param[0], param[1]);
		}
	}

}